#include "../main.h"

/* 
--------------------------------------------------------------------------------
	函数功能：目标ID与本机ID不匹配
--------------------------------------------------------------------------------
*/
int system_key_error(int sockfd, char *tagid)
{
	int ret = 0;
	char data[1024];
	cJSON *pRoot = NULL, *pRow = NULL;

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_DEVICE_KEY_ERROR");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", "");

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：路由器注册
--------------------------------------------------------------------------------
*/
int system_register(int sockfd)
{
	int ret = 0;
	char data[1024];
	cJSON *pRoot = NULL, *pRow = NULL;

	pRow = cJSON_CreateObject();
	cJSON_AddStringToObject(pRow, "ip", Server);
	cJSON_AddStringToObject(pRow, "protol", "tcp");
	cJSON_AddNumberToObject(pRow, "port", Port);
	cJSON_AddStringToObject(pRow, "publickey", RouteID);
	cJSON_AddBoolToObject(pRow, "bRouter", 1);

	char value[128];
	memset(value, 0, sizeof(value));
	ret = get_value("web.system.fwver", &value);
	if (ret == 1 && strlen(value))
	{
		cJSON_AddStringToObject(pRow, "version", value);
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "REGISTER");
	cJSON_AddBoolToObject(pRoot, "bNew", 1);
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", ServerID);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_PrintUnformatted(pRoot);

	if (pRoot != NULL)
	{
		cJSON_Delete(pRoot);
	}
	if (buffer != NULL)
	{
		free(buffer);
		buffer = NULL;
	}
	ret = send_msg(sockfd, data, strlen(data));
	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：注册包返回
--------------------------------------------------------------------------------
*/
int system_register_response(int sockfd, char *tagid)
{
	int ret = 0;
	char data[1024];
	cJSON *pRoot = NULL;

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "REGISTER_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "REGISTER");
	cJSON_AddBoolToObject(pRoot, "bNew", 0);
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", "");

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：获取FCN网卡IP地址
--------------------------------------------------------------------------------
*/
int system_fcnip()
{
	char value[128];

	memset(value, 0, sizeof(value));
	shell_cmd("ifconfig ftun0 2>/dev/null | grep 'inet addr' | awk -F 'addr:' '{print $2}' | awk '{print $1}'", value);

	if (strlen(value) > 7 && strlen(value) < 16)
	{
		memset(FCNIP, 0, sizeof(FCNIP));
		memcpy(FCNIP, value, strlen(value));
		return 0;
	}

	return 1;
}

/* 
--------------------------------------------------------------------------------
	函数功能：路由器心跳
--------------------------------------------------------------------------------
*/
int system_ping(int sockfd)
{
	int ret = 0;
	char data[1024];
	cJSON *pRoot = NULL, *pRow = NULL;

	pRow = cJSON_CreateObject();
	cJSON_AddStringToObject(pRow, "ip", FCNIP);
	cJSON_AddNumberToObject(pRow, "port", 80);
	cJSON_AddStringToObject(pRow, "publickey", RouteID);
	cJSON_AddBoolToObject(pRow, "bRouter", 1);

	//printf("bRouter:1\n");
	char value[128];
	memset(value, 0, sizeof(value));
	ret = get_value("web.system.fwver", &value);
	if (ret == 1 && strlen(value))
	{
		cJSON_AddStringToObject(pRow, "version", value);
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "PING");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", ServerID);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
	{
		cJSON_Delete(pRow);
	}

	if (pRoot != NULL)
	{
		cJSON_Delete(pRoot);
	}

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：心跳返回
--------------------------------------------------------------------------------
*/
int system_ping_response(int sockfd, char *tagid)
{
	int ret = 0;
	char data[1024];
	cJSON *pRoot = NULL;

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "PING_REPLY");
	cJSON_AddStringToObject(pRoot, "replyType", "PING");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", "");

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：路由器WAN速率
--------------------------------------------------------------------------------
*/
int system_get_wanspeed(int sockfd, char *tagid)
{
	int ret = 0;
	cJSON *pRoot = NULL;
	char *result = shell_cmd_change("lua /usr/sbin/wanspeed.lua");
	if (result != NULL)
	{
		pRoot = cJSON_CreateObject();
		cJSON_AddStringToObject(pRoot, "type", "DEVICE_STATUS_UPANDDOWNSIZE_RSP");
		cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_STATUS_UPANDDOWNSIZE_REQ");
		cJSON_AddStringToObject(pRoot, "clientId", RouteID);
		cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
		cJSON_AddStringToObject(pRoot, "content", result);
		char *buffer = cJSON_PrintUnformatted(pRoot);

		if (pRoot != NULL)
		{
			cJSON_Delete(pRoot);
		}
		ret = send_msg(sockfd, buffer, strlen(buffer));

		delete_mem(buffer);
		delete_mem(result);

		return ret;
	}
	else
	{
		return -1;
	}
}

/* 
--------------------------------------------------------------------------------
	函数功能：当前网络
--------------------------------------------------------------------------------
*/
int system_get_wantatus(int sockfd, char *tagid)
{
	int ret = 0;
	char data[1024];
	cJSON *pRoot = NULL;

	memset(data, 0, sizeof(data));
	shell_cmd("/usr/sbin/wanstatus.sh", data);

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_STATUS_CURRENT_REP");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_STATUS_CURRENT_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", data);

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRoot != NULL)
	{
		cJSON_Delete(pRoot);
	}

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：路由信息
--------------------------------------------------------------------------------
*/
int system_info(int sockfd, char *tagid)
{
	int ret = 0;
	int enable = 0;
	cJSON *pRow = NULL, *pRoot = NULL;
	pRow = cJSON_CreateObject();
	char *proto = get_value_change("network.wan.proto");
	char *fwver = get_value_change("web.system.fwver");
	char *model = get_value_change("web.system.model");
	char *blackcount = shell_cmd_change("uci show filter | grep 'deny' | grep 'mac' | wc -l");
	char *enable_result = get_value_change("filter.list.enable");
	char *rule = get_value_change("filter.list.rule");
	char *bshare = get_value_change("appctrl.@appctrl[0].share");
	if (proto != NULL)
	{
		cJSON_AddStringToObject(pRow, "proto", proto);
	}
	if (fwver != NULL)
	{
		cJSON_AddStringToObject(pRow, "version", fwver);
	}
	if (model != NULL)
	{
		cJSON_AddStringToObject(pRow, "name", model);
	}
	if (blackcount != NULL && strlen(blackcount) > 0)
	{
		cJSON_AddNumberToObject(pRow, "blackcount", atoi(blackcount));
	}
	if (enable_result != NULL && strlen(enable_result))
	{
		enable = atoi(enable_result);
	}
	if (rule != NULL && strlen(rule))
	{
		if (enable == 1 && !strcmp(rule, "deny"))
		{
			cJSON_AddBoolToObject(pRow, "blackStatus", 1);
		}
		else
		{
			cJSON_AddBoolToObject(pRow, "blackStatus", 0);
		}
	}
	if (bshare != NULL && strlen(bshare))
	{
		if (atoi(bshare) && strlen(DiskPath))
		{
			cJSON_AddBoolToObject(pRow, "bShareDisk", 1);
		}
		else
		{
			cJSON_AddBoolToObject(pRow, "bShareDisk", 0);
		}
	}
	else
	{
		cJSON_AddBoolToObject(pRow, "bShareDisk", 0);
	}
	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_LOADING_REP");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_LOADING_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);

	
	delete_mem(proto);
	delete_mem(fwver);
	delete_mem(model);
	delete_mem(blackcount);
	delete_mem(enable_result);
	delete_mem(rule);
	delete_mem(bshare);

	char *content = cJSON_PrintUnformatted(pRow);
	content[strlen(content)] = '\0';
	cJSON_AddStringToObject(pRoot, "content", content);

	char *buffer = cJSON_PrintUnformatted(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRoot != NULL)
	{
		cJSON_Delete(pRoot);
	}
	ret = send_msg(sockfd, buffer, strlen(buffer));
	delete_mem(content);
	delete_mem(buffer);
	return ret;
}